//
//  MainGame.cpp
//  KRoll
//
//  Created by VIETHUNG on 3/13/14.
//
//

#include "MainGame.h"
#include "SimpleAudioEngine.h"
#include "VisibleRect.h"
#include "config.h"

USING_NS_CC;
using namespace CocosDenshion;

MainGame::~MainGame()
{
    
}

CCScene* MainGame::scene()
{
    // 'scene' is an autorelease object
    CCScene* scene = CCScene::create();
    
    // 'layer' is an autorelease object
    MainGame *layer = MainGame::create();
    
    // add layer as a child to scene
    scene->addChild(layer);
    
    return scene;
}

void MainGame::onEnter()
{
    CCDirector* pDirector = CCDirector::sharedDirector();
    pDirector->getTouchDispatcher()->addTargetedDelegate(this, 0, true);
    border = VisibleRect::right().x / 2;
    gameStatus = kGamePause;
    gameStart();
    CCLayer::onEnter();    
}

void MainGame::onExit()
{
    CCDirector* pDirector = CCDirector::sharedDirector();
    pDirector->getTouchDispatcher()->removeDelegate(this);
    CCLayer::onExit();
}

bool MainGame::init()
{
    if (!CCLayer::init()) {
        return false;
    }

    Background::init();
    
    this->resetClouds();
    
    //set score
    score = 0;
    
    //get screen original
    sizeVisi = VisibleRect::getVisibleRect().size;
    
    //get screen size
    sizeScreen = CCDirector::sharedDirector()->getWinSize();
    
    this->setAccelerometerEnabled(true);
    this->scheduleUpdate();
    
    return true;
}

void MainGame::menuCloseCallback(cocos2d::CCObject *pSender)
{
    
}

void MainGame::update(float dt)
{
    Background::step(dt);
    if (gameStatus != kGamePause) {
        CCLOG(" update");
    }
}



void MainGame::gameStart()
{
    /* play game */
    lblPlay = CCLabelTTF::create("Play Game", "Marker Felt.ttf", 30);
    CCMenuItemLabel *restartItem = CCMenuItemLabel::create(lblPlay, this, menu_selector(MainGame::playGame));
    restartItem->setPosition(VisibleRect::center());
    CCMenu *menu = CCMenu::create(restartItem, NULL);
    menu->setPosition(CCPointZero);
    this->addChild(menu);
    
    
}

void MainGame::playGame()
{
    gameStatus = kGameStart;
    lblPlay->setVisible(false);
    this->tutorial();
}

void MainGame::tutorial()
{
    /*
    CCSprite *singleTap = CCSprite::create("Single_Tap.png");
    singleTap->setPosition(ccp(sizeVisi.width / 2 , sizeVisi.height / 4));
    addChild(singleTap);
    CCMoveBy *moveByRight = CCMoveBy::create(2.0f, ccp(sizeVisi.width / 3, 0));
    singleTap->runAction(CCRepeat::create(moveByRight, 1));
    CCMoveBy *moveByLeft = CCMoveBy::create(2.0f, ccp( - sizeVisi.width / 3, 0));
    singleTap->runAction(CCRepeat::create(moveByLeft, 1));
    */
}

void MainGame::gameOver()
{
    
}

/*
void MainGame::addLabelScore()
{
    CCString* strScore = CCString::createWithFormat("%d",score);
    labelScore = CCLabelTTF::create(strScore->getCString(), "Marker Felt.ttf", 30);
    labelScore->setFontFillColor(ccGRAY);
    labelScore->setZOrder(getChildrenCount());
    labelScore->setPosition(ccp(VisibleRect::right().x / 2, VisibleRect::top().y - labelScore->getContentSize().height));
    this->addChild(labelScore);
}

void MainGame::updateUIScore()
{
    CCString* strp = CCString::createWithFormat("%d", score);
    labelScore->setString(strp->getCString());
}
*/
#pragma mark - Add Scene
void MainGame::addBackGround()
{
    CCSprite* bg = CCSprite::create("background.png");
    bg->setPosition(VisibleRect::center());
    this->addChild(bg, kBackground);
}

void MainGame::addCloud()
{
    
}

void MainGame::addBlock()
{
    
}

#pragma mark - accelerate
void MainGame::didAccelerate(CCAcceleration* pAccelerationValue){
    
    if(gameStatus == kGamePause) return;
	float accel_filter = 0.1f;
	bird_vel.x = bird_vel.x * accel_filter + pAccelerationValue->x * (1.0f - accel_filter) * 500.0f;
    CCLog("Game didAccelerate  bird_vel.x = %f", bird_vel.x);
}


#pragma mark - touch
bool MainGame::ccTouchBegan(CCTouch *touch, CCEvent *event)
{
    if (gameStatus == kGamePause) return true;
    CCPoint touchPoint = touch->getLocation();
    CCLOG("%f ==== %f",touchPoint.x,border);
    if (touchPoint.x > border) {
        pointMove += kMove; // move right
    } else {
        pointMove -= kMove; // move left
    }
    CCLOG("%f",pointMove);
    return true;
}

